package com.zm.zmgz.service;

import com.zm.zmgz.dao.BusinessAnalysisBeanMapper;
import com.zm.zmgz.pojo.*;
import com.zm.zmgz.pojo.ChartDataRows.ChartDataRows;
import com.zm.zmgz.pojo.ChartDataRows.StaffProfitChartDataRows;
import com.zm.zmgz.utils.BigDecimalUtils;
import com.zm.zmgz.utils.DateUtils;
import com.zm.zmgz.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/05/27/17:44
 * @Description:营业分析业务层
 */
@Service
public class BusinessAnalysisService {

    @Autowired
    BusinessAnalysisBeanMapper businessAnalysisBeanMapper; // 营业分析表

    @Autowired
    ProjectStatisticsService projectStatisticsService; // 项目统计表

    @Autowired
    EmployeeStatisticsService employeeStatisticsService; // 员工统计表

    @Autowired
    RevenueStatisticsService revenueStatisticsService; // 收支统计表业务层

    /***
     * @Description: 通过批次刷新营业分析记录
     *删除营业分析表,项目统计表,收支统计表,员工统计表记录(前提:删除前,获取营业分析表的id)
     * 生成营业分析表,项目统计表,收支统计表,员工统计表记录(前提:添加前,获取营业分析表的id)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 17:45:50
     */
    @Transactional
    public Boolean refreshBusinessAnalysisByBatch(String batch) {
        // 获取该批次的batchId;
        Integer id = getIdByBatch(batch);
        // 如果该批次存在,就删除旧的记录
        if (!Objects.isNull(id)) {
            // 删除营业分析表记录
            delteBusinessAnalysisByBatch(batch);
            // 删除项目统计表记录
            projectStatisticsService.delteProjectStatisticsByBatch(id);
            // 删除员工统计表记录
            employeeStatisticsService.delteEmployeeStatisticsByBatch(id);
            // 删除收支统计表记录
            revenueStatisticsService.delteRevenueStatisticsByBatch(id);

        }

        // 添加营业分析表记录
        addBusinessAnalysisByBatch(batch);
        // 获取该批次新Id
        Integer id1 = getIdByBatch(batch);
        // 判断该批次营业分析记录是否新建成功!
        if (Objects.isNull(id1))
            return false;
        // 添加项目统计表记录
        projectStatisticsService.addProjectStatisticsByBatchId(id1);
        // 添加员工统计表记录
        employeeStatisticsService.addEmployeeStatisticsByBatchId(id1);
        // 添加收支统计表记录
        revenueStatisticsService.addRevenueStatisticsByBatchId(id1);
        return true;
    }

    /***
     * @Description: 通过批次来添加营业分析记录
     * 批次直接赋值,主键自增不用管
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 18:07:48
     */
    public int addBusinessAnalysisByBatch(String batch) {
        BusinessAnalysisBean businessAnalysisBean = new BusinessAnalysisBean();
        businessAnalysisBean.setBatch(batch);
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        businessAnalysisBean.setCreatedate(date);
        return businessAnalysisBeanMapper.insert(businessAnalysisBean);
    }

    /***
     * @Description:通过batch获取batchId
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 17:59:35
     */
    public Integer getIdByBatch(String batch) {
        BusinessAnalysisBeanExample businessAnalysisBeanExample = new BusinessAnalysisBeanExample();
        BusinessAnalysisBeanExample.Criteria criteria = businessAnalysisBeanExample.createCriteria();
        criteria.andBatchEqualTo(batch);
        List<BusinessAnalysisBean> businessAnalysisBeans = businessAnalysisBeanMapper.selectByExample(businessAnalysisBeanExample);
        if (Objects.isNull(businessAnalysisBeans) || businessAnalysisBeans.size() == 0)
            return null;
        return businessAnalysisBeans.get(0).getId();

    }

    /***
     * @Description: 通过batch删除营业分析表记录
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 17:51:48
     */
    public int delteBusinessAnalysisByBatch(String batch) {
        BusinessAnalysisBeanExample businessAnalysisBeanExample = new BusinessAnalysisBeanExample();
        BusinessAnalysisBeanExample.Criteria criteria = businessAnalysisBeanExample.createCriteria();
        criteria.andBatchEqualTo(batch);
        return businessAnalysisBeanMapper.deleteByExample(businessAnalysisBeanExample);
    }


    /***
     * @Description: 通过Batch获取Id
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-27 18:28:11
     */
    public String getBatchById(Integer id) {
        BusinessAnalysisBean businessAnalysisBean = businessAnalysisBeanMapper.selectByPrimaryKey(id);
        if (Objects.isNull(businessAnalysisBean))
            return null;
        return businessAnalysisBean.getBatch();

    }


    /***
     * @Description: 获取批次选择数据
     * - 获取全部记录(排序:填写日期升序;"createDate ASC")
     * - 遍历全部记录，创建数据对象集合
     *   - 获取时间，提取年，年级中是否存在该年
     *     - 有则提取月，判断月级中是否存在该月
     *       - 有则舍弃（看你是否要日级）
     *       - 无则创建该月级
     *     - 无则创建该年级
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-21 21:36:15
     */
    public List<ProjectProfitBatchOptionBean> getBatchOptions() {
        // 获取全部记录(排序:批次升序,项目名升序,利润降序;creationDate ASC, itemName asc,profit DESC)
        String order = "createDate ASC";
        List<BusinessAnalysisBean> list = getAllAndOrder(order);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        // 遍历全部记录，创建数据对象集合,进行解析
        List<ProjectProfitBatchOptionBean> list1 = new ArrayList<>();
        for (BusinessAnalysisBean businessAnalysisBean : list) {
            String batch = businessAnalysisBean.getBatch();
            Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
            String year = DateUtils.getDateString(date, "YYYY年");
            String month = DateUtils.getDateString(date, "MM月");
            // 搜索是否存在该年;如果不存在就添加该年该月
            int i = 0;
            for (ProjectProfitBatchOptionBean bean : list1) {
                // 该年存在
                if (bean.getValue().equals(year)) {
                    // 搜索是否存在该月
                    List<ProjectProfitBatchOptionBean> list2 = bean.getChildren();
                    int j = 0;
                    // 遍历该年是否存在该月
                    for (ProjectProfitBatchOptionBean optionBean : list2) {
                        if (optionBean.getLabel().equals(month)) {
                            j++;
                            break;
                        }
                    }
                    // 该年存在,但该月不存在,添加月
                    if (j == 0) {
                        ProjectProfitBatchOptionBean optionBean1 = new ProjectProfitBatchOptionBean();
                        optionBean1.setValue(year + month);
                        optionBean1.setLabel(month);
                        bean.getChildren().add(optionBean1);
                    }
                    i++;
                    break;
                }
            }
            // 该年不存在
            if (i == 0) {
                ProjectProfitBatchOptionBean optionBean = new ProjectProfitBatchOptionBean();
                optionBean.setLabel(year);
                optionBean.setValue(year);
                optionBean.setChildren(new ArrayList<>());
                ProjectProfitBatchOptionBean optionBean1 = new ProjectProfitBatchOptionBean();
                optionBean1.setValue(year + month);
                optionBean1.setLabel(month);
                optionBean.getChildren().add(optionBean1);
                list1.add(optionBean);
            }

        }
        return list1;
    }


    /***
     * @Description: 获取全部数据并按要求排序
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-22 03:00:35
     */
    public List<BusinessAnalysisBean> getAllAndOrder(String order) {
        BusinessAnalysisBeanExample projectProfitBeanExample = new BusinessAnalysisBeanExample();
        projectProfitBeanExample.setOrderByClause(order);
        List<BusinessAnalysisBean> list = businessAnalysisBeanMapper.selectByExample(projectProfitBeanExample);
        return list;
    }

    /***
     * @Description: 获取批次选择数据(遍历项目统计表 ; 新项目, 旧项目, 休眠项目)
     * 从批次中获取上批次,判断是否存在上批次,如果不存在,设置title,返回.
     * 从批次中获取批次id,得到该批次项目统计记录
     * 通过上批次与本批次对比,得到数据,进行装填.
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-29 10:18:48
     */
    public PieChart getprojectChangesData(String batch) {
        // 获取上批次BusinessAnalysisBean对象
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        Date lastMonth = DateUtils.getLastMonth(date);
        String lastBatch = DateUtils.getDateString(lastMonth, "yyyy年MM月");
        BusinessAnalysisBean lastBusinessAnalysisBean = getBusinessAnalysisByBatch(lastBatch);
        PieChart pieChart = new PieChart();
        // 无上批次BusinessAnalysisBean对象
        if (Objects.isNull(lastBusinessAnalysisBean)) {
            pieChart.setTitle("项目变更(与上月对比)" + "暂无上批次数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 获取本批次BusinessAnalysisBean对象
        BusinessAnalysisBean businessAnalysisBean = getBusinessAnalysisByBatch(batch);
        if (Objects.isNull(businessAnalysisBean)) {
            pieChart.setTitle("项目变更(与上月对比)" + "暂无本批次数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 获取本批次和上批次的项目盈利表对象集合
        List<ProjectStatisticsBean> lastList = projectStatisticsService.getProjectStatisticsBeansByBatchId(lastBusinessAnalysisBean.getId());
        List<ProjectStatisticsBean> list = projectStatisticsService.getProjectStatisticsBeansByBatchId(businessAnalysisBean.getId());
        // 项目统计对象集合不存在
        if (Objects.isNull(lastList) || Objects.isNull(list)) {
            pieChart.setTitle("暂无项目统计数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 设置详细数据
        List<List<String>> lists = new ArrayList<>();
        // 设置饼图数据(列,行)
        PieChartData pieChartData = new PieChartData();
        // 设置行
        List<PieChartDataRows> rows = new ArrayList<>();
        // 设置列
        List<String> list1 = new ArrayList<>();
        list1.add("options");
        list1.add("number");
        pieChartData.setColumns(list1);
        // 设置新项目详细数据和新项目行(本批次中存在,上批次中不存在)
        List<String> newProject = new ArrayList<>();
        PieChartDataRows newProjectPieChartDataRows = new PieChartDataRows();
        int i = 0;
        for (ProjectStatisticsBean bean : list) {
            String itemname = bean.getItemname();
            Boolean b = new Boolean(false);
            // 从上批次中查询是否存在该项目
            for (ProjectStatisticsBean statisticsBean : lastList) {
                if (statisticsBean.getItemname().equals(itemname)) {
                    b = true;
                    break;
                }
            }
            // 上批次不存在该项目;添加到新项目集合中
            if (!b) {
                newProject.add(itemname);
                i++;
            }
        }
        newProjectPieChartDataRows.setNumber(i * 1.0);
        newProjectPieChartDataRows.setOptions("新项目");
        rows.add(newProjectPieChartDataRows);
        lists.add(newProject);

        // 设置旧项目详细数据和旧项目行(本批次中存在,上批次中也存在)
        List<String> oldProject = new ArrayList<>();
        PieChartDataRows oldProjectPieChartDataRows = new PieChartDataRows();
        i = 0;
        for (ProjectStatisticsBean bean : lastList) {
            String itemname = bean.getItemname();
            Boolean b = new Boolean(false);
            // 从本批次中查询是否存在该项目
            for (ProjectStatisticsBean statisticsBean : list) {
                if (statisticsBean.getItemname().equals(itemname)) {
                    b = true;
                    break;
                }
            }
            // 本批次中存在,上批次中也存在;添加到旧项目集合中
            if (b) {
                oldProject.add(itemname);
                i++;
            }
        }
        oldProjectPieChartDataRows.setNumber(i * 1.0);
        oldProjectPieChartDataRows.setOptions("旧项目");
        rows.add(oldProjectPieChartDataRows);

        // 设置休眠项目详细数据和休眠项目行(本批次中不存在,上批次中存在)
        List<String> sleepProject = new ArrayList<>();
        PieChartDataRows sleepProjectPieChartDataRows = new PieChartDataRows();
        i = 0;
        for (ProjectStatisticsBean bean : lastList) {
            String itemname = bean.getItemname();
            Boolean b = new Boolean(false);
            // 从本批次中查询是否存在该项目
            for (ProjectStatisticsBean statisticsBean : list) {
                if (statisticsBean.getItemname().equals(itemname)) {
                    b = true;
                    break;
                }
            }
            // 本批次中不存在,上批次中存在;添加到休眠项目集合中
            if (!b) {
                sleepProject.add(itemname);
                i++;
            }
        }
        lists.add(sleepProject);
        sleepProjectPieChartDataRows.setNumber(i * 1.0);
        sleepProjectPieChartDataRows.setOptions("休眠项目");
        rows.add(sleepProjectPieChartDataRows);
        pieChartData.setRows(rows);
        pieChart.setLists(lists);
        pieChart.setChartData(pieChartData);
        pieChart.setTitle("项目变更(与上月对比)");
        return pieChart;
    }

    /***
     * @Description: 通过批次获取BusinessAnalysis对象
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-29 10:38:44
     */
    private BusinessAnalysisBean getBusinessAnalysisByBatch(String batch) {
        BusinessAnalysisBeanExample beanExample = new BusinessAnalysisBeanExample();
        BusinessAnalysisBeanExample.Criteria criteria = beanExample.createCriteria();
        criteria.andBatchEqualTo(batch);
        List<BusinessAnalysisBean> list = businessAnalysisBeanMapper.selectByExample(beanExample);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        return list.get(0);
    }

    /***
     * @Description: 获取标题
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-29 14:57:21
     */
    public String getTile(String batch) {
        return batch + "营业分析";
    }


    /***
     * @Description: 获取批次选择数据(遍历员工统计表 ; 停职人员, 入职人员, 转正人员)
     * 从批次中获取上批次,判断是否存在上批次,如果不存在,设置title,返回.
     * 从批次中获取批次id,得到该批次项目统计记录
     * 通过上批次与本批次对比,得到数据,进行装填.
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-29 10:18:48
     * 08点58分 - 09点56分
     */
    public PieChart getStaffChangesData(String batch) {
        // 获取上批次BusinessAnalysisBean对象
        Date date = DateUtils.getDateByString(batch, "yyyy年MM月");
        Date lastMonth = DateUtils.getLastMonth(date);
        String lastBatch = DateUtils.getDateString(lastMonth, "yyyy年MM月");
        BusinessAnalysisBean lastBusinessAnalysisBean = getBusinessAnalysisByBatch(lastBatch);
        PieChart pieChart = new PieChart();
        // 无上批次BusinessAnalysisBean对象
        if (Objects.isNull(lastBusinessAnalysisBean)) {
            pieChart.setTitle("人员变更(与上月对比)" + "暂无上批次数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 获取本批次BusinessAnalysisBean对象
        BusinessAnalysisBean businessAnalysisBean = getBusinessAnalysisByBatch(batch);
        if (Objects.isNull(businessAnalysisBean)) {
            pieChart.setTitle("人员变更(与上月对比)" + "暂无本批次数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 根据批次id获取上批次和本批次员工统计记录
        List<EmployeeStatisticsBean> list = employeeStatisticsService.getEmployeeStatisticsByBatchId(businessAnalysisBean.getId());
        List<EmployeeStatisticsBean> lastList = employeeStatisticsService.getEmployeeStatisticsByBatchId(lastBusinessAnalysisBean.getId());
        if (Objects.isNull(list)) {
            pieChart.setTitle("人员变更(与上月对比)" + "暂无本批次员工统计数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        if (Objects.isNull(lastList)) {
            pieChart.setTitle("人员变更(与上月对比)" + "暂无上批次员工统计数据!");
            pieChart.setDataEmpty(true);
            return pieChart;
        }
        // 创建详细数据和3行
        List<List<String>> lists = new ArrayList<>();
        List<String> newList = new ArrayList<>();
        List<String> correctList = new ArrayList<>();
        List<String> evacuateList = new ArrayList<>();
        // 创建饼图数据,列,3行
        PieChartData pieChartData = new PieChartData();
        List<String> columns = new ArrayList<>(); // 列
        columns.add("options");
        columns.add("number");
        pieChartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>(); // 4行
        PieChartDataRows newEmp = new PieChartDataRows();
        newEmp.setOptions("新员工");
        PieChartDataRows correctEmp = new PieChartDataRows();
        correctEmp.setOptions("转正员工");
        PieChartDataRows evacuateEmp = new PieChartDataRows();
        evacuateEmp.setOptions("撤离员工");
        PieChartDataRows stableEmp = new PieChartDataRows();
        stableEmp.setOptions("稳定员工");
        // 创建存储3中不稳定员工的姓名
        List<String> noStable = new ArrayList<>();
        // 获取新员工数据(上批次不存在,本批次存在);填充newList,newEmp(number)
        // 算法:遍历本批次数据,通过姓名从上批次中获取记录
        Double number = new Double(0);
        for (EmployeeStatisticsBean bean : list) {
            String name = bean.getName();
            EmployeeStatisticsBean bean1 = employeeStatisticsService.getEmpStatisticsByNameFromList(name, lastList);
            if (Objects.isNull(bean1)) {
                noStable.add(name);
                newList.add(name);
                number++;
            }
        }
        lists.add(newList);
        newEmp.setNumber(number);
        rows.add(newEmp);
        // 获取转正员工数据(上批次为试用,本批次为转正);填充correctEmp,correctList
        // 算法:遍历上批次数据,获取状态为试用的记录,通过姓名从上批次中获取记录,判断状态是否为转正
        number = new Double(0);
        for (EmployeeStatisticsBean bean : lastList) {
            String status = bean.getStatus();
            if (!status.equals("试用"))
                continue;
            String name = bean.getName();
            EmployeeStatisticsBean bean1 = employeeStatisticsService.getEmpStatisticsByNameFromList(name, list);
            if (Objects.isNull(bean1))
                continue;
            String status1 = bean1.getStatus();
            if (status1.equals("转正")) {
                noStable.add(name);
                correctList.add(name);
                number++;
            }
        }
        lists.add(correctList);
        correctEmp.setNumber(number);
        if (number > 0) {
            rows.add(correctEmp);
        }
        // 获取撤离员工数据(上批次存在,本批次不存在);填充evacuateList,evacuateEmp
        // 算法:遍历上批次数据,通过姓名从本批次中获取记录
        number = new Double(0);
        for (EmployeeStatisticsBean bean : lastList) {
            String name = bean.getName();
            EmployeeStatisticsBean bean1 = employeeStatisticsService.getEmpStatisticsByNameFromList(name, list);
            if (Objects.isNull(bean1)) {
                noStable.add(name);
                evacuateList.add(name);
                number++;
            }
        }
        lists.add(evacuateList);
        evacuateEmp.setNumber(number);
        if (number > 0) {
            rows.add(evacuateEmp);
        }
        // 获取稳定员工数据(上批次存在,本批次存在,并且非入职,非转正,非离职员工);填充stableEmp
        // 算法:遍历上批次数据;通过姓名从上批次中获取记录;判断该姓名是否存在以上生成的数组中
        number = new Double(0);
        for (EmployeeStatisticsBean bean : lastList) {
            String name = bean.getName();
            EmployeeStatisticsBean bean1 = employeeStatisticsService.getEmpStatisticsByNameFromList(name, list);
            if (Objects.isNull(bean1))
                continue;
            Boolean aBoolean = ListUtils.isExistStringFromList(name, noStable);
            number++;
        }
        stableEmp.setNumber(number);
        if (number > 0) {
            rows.add(stableEmp);
        }
        // 设置标题,饼图数据,详细数据
        pieChartData.setRows(rows);
        pieChart.setTitle("人员变更(与上月对比)");
        pieChart.setLists(lists);
        pieChart.setDataEmpty(false);
        pieChart.setChartData(pieChartData);
        return pieChart;
    }

    /***
     * @Description: 获取项目收入数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次项目统计记录是否存在
     * 遍历项目统计记录(一条记录为一行)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getProjectIncomeData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("项目收入(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<ProjectStatisticsBean> beanList = projectStatisticsService.getProjectStatisticsBeansByBatchId(batchId);
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("项目收入(暂无该批次项目统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(项目总收入),饼图数据(列,行)
        pieChart.setTitle("项目收入");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> totalProjectIncome = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();
        // 遍历项目统计记录(一条记录为一行)
        for (ProjectStatisticsBean bean : beanList) {
            PieChartDataRows pieChartDataRows = new PieChartDataRows();
            // 设置项目名
            String itemname = bean.getItemname();
            pieChartDataRows.setOptions(itemname);
            // 设置项目收入
            BigDecimal decimal = bean.getProjectincome();
            pieChartDataRows.setNumber(decimal.doubleValue());
            // 添加行(支出不等于0,才添加到饼图中)
            if (decimal.doubleValue() != 0) {
                rows.add(pieChartDataRows);
            }
            // 累加到总收入
            bigDecimal = BigDecimalUtils.add(decimal, bigDecimal);
        }
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        String s = bigDecimal.toString();
        totalProjectIncome.add(s);
        lists.add(totalProjectIncome);
        pieChart.setLists(lists);
        return pieChart;
    }

    /***
     * @Description: 判断该批次营业分析记录是否存在, 如果存在返回批次id(id)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:21:52
     */
    public Integer existBatchByBatch(String batch) {
        BusinessAnalysisBeanExample example = new BusinessAnalysisBeanExample();
        BusinessAnalysisBeanExample.Criteria criteria = example.createCriteria();
        criteria.andBatchEqualTo(batch);
        List<BusinessAnalysisBean> list = businessAnalysisBeanMapper.selectByExample(example);
        if (Objects.isNull(list) || list.size() == 0)
            return null;
        BusinessAnalysisBean bean = list.get(0);
        return bean.getId();

    }


    /***
     * @Description: 获取项目支出数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次项目统计记录是否存在
     * 遍历项目统计记录(一条记录为一行)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getProjectExpenditureData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("项目支出(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<ProjectStatisticsBean> beanList = projectStatisticsService.getProjectStatisticsBeansByBatchId(batchId);
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("项目支出(暂无该批次项目统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(项目总收入),饼图数据(列,行)
        pieChart.setTitle("项目支出");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> totalProjectIncome = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();
        // 遍历项目统计记录(一条记录为一行)
        for (ProjectStatisticsBean bean : beanList) {
            PieChartDataRows pieChartDataRows = new PieChartDataRows();
            // 设置项目名
            String itemname = bean.getItemname();
            pieChartDataRows.setOptions(itemname);
            // 设置项目支出
            BigDecimal decimal = bean.getProjectexpenditure();
            pieChartDataRows.setNumber(decimal.doubleValue());
            // 添加行(支出不等于0,才添加到饼图中)
            if (decimal.doubleValue() != 0) {
                rows.add(pieChartDataRows);
            }
            // 累加到总支出
            bigDecimal = BigDecimalUtils.add(decimal, bigDecimal);
        }
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        String s = bigDecimal.toString();
        totalProjectIncome.add(s);
        lists.add(totalProjectIncome);
        pieChart.setLists(lists);
        return pieChart;
    }


    /***
     * @Description: 获取项目利润数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次项目统计记录是否存在
     * 遍历项目统计记录(一条记录为一行)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getProjectProfitData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("项目利润(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<ProjectStatisticsBean> beanList = projectStatisticsService.getProjectStatisticsBeansByBatchId(batchId);
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("项目利润(暂无该批次项目统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(项目总收入),饼图数据(列,行)
        pieChart.setTitle("项目利润");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> totalProjectIncome = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();
        // 遍历项目统计记录(一条记录为一行)
        for (ProjectStatisticsBean bean : beanList) {
            PieChartDataRows pieChartDataRows = new PieChartDataRows();
            // 设置项目名
            String itemname = bean.getItemname();
            pieChartDataRows.setOptions(itemname);
            // 设置项目利润
            BigDecimal decimal = bean.getProjectprofit();
            pieChartDataRows.setNumber(decimal.doubleValue());
            // 添加行(利润不等于0,才添加到饼图中)
            if (decimal.doubleValue() != 0) {
                rows.add(pieChartDataRows);
            }
            // 累加到总利润
            bigDecimal = BigDecimalUtils.add(decimal, bigDecimal);
        }
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        String s = bigDecimal.toString();
        totalProjectIncome.add(s);
        lists.add(totalProjectIncome);
        pieChart.setLists(lists);
        return pieChart;
    }


    /***
     * @Description: 获取项目利润比数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次项目统计记录是否存在
     * 获取批次总收入,总支出
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getProjectProfitRatioData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("项目利润比(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<ProjectStatisticsBean> beanList = projectStatisticsService.getProjectStatisticsBeansByBatchId(batchId);
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("项目利润比(暂无该批次项目统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(项目总收入),饼图数据(列,行)
        pieChart.setTitle("项目利润比");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> projectProfitRatio = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();

        // 设置项目总收入
        PieChartDataRows row = new PieChartDataRows();
        BigDecimal decimal = projectStatisticsService.getTotalProjectIncomeByBatchId(batchId); // 项目总收入
        row.setNumber(decimal.doubleValue());
        row.setOptions("项目总收入");
        rows.add(row);
        // 获取项目总支出
        PieChartDataRows row2 = new PieChartDataRows();
        BigDecimal decimal2 = projectStatisticsService.getTotalProjectExpenditureByBatchId(batchId); // 项目总支出
        row2.setNumber(decimal2.doubleValue());
        row2.setOptions("项目总支出");
        rows.add(row2);
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        BigDecimal bigDecimal1 = decimal.divide(decimal2, 4).multiply(new BigDecimal(100)); // 项目收支利润比
        String s = bigDecimal1.toString();
        s = s.substring(0, s.length() - 3);
        projectProfitRatio.add(s);
        lists.add(projectProfitRatio);
        pieChart.setLists(lists);
        return pieChart;
    }

    /***
     * @Description: 获取支出数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次收支统计记录是否存在
     * 遍历收支统计记录(一条记录为一行)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getExpenditureData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("支出(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<RevenueStatisticsBean> beanList = revenueStatisticsService.getRevenueStatisticsByBatchIdAndRevenueKind(batchId, "支出");
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("支出(暂无该批次支出统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(总支出),饼图数据(列,行)
        pieChart.setTitle("支出");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> totalProjectIncome = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();
        // 遍历项目统计记录(一条记录为一行)
        for (RevenueStatisticsBean bean : beanList) {
            PieChartDataRows pieChartDataRows = new PieChartDataRows();
            // 设置种类
            String kind = bean.getKind();
            pieChartDataRows.setOptions(kind);
            // 设置数目
            BigDecimal decimal = bean.getAmount();
            pieChartDataRows.setNumber(decimal.doubleValue());
            // 添加行(收入不等于0,才添加到饼图中)
            if (decimal.doubleValue() != 0) {
                rows.add(pieChartDataRows);
            }
            // 累加到总支出
            bigDecimal = BigDecimalUtils.add(decimal, bigDecimal);
        }
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        String s = bigDecimal.toString();
        totalProjectIncome.add(s);
        lists.add(totalProjectIncome);
        pieChart.setLists(lists);
        return pieChart;
    }

    /***
     * @Description: 获取收入数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次收支统计记录是否存在
     * 遍历收支统计记录(一条记录为一行)
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getRevenueData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("收入(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次项目统计记录是否存在
        List<RevenueStatisticsBean> beanList = revenueStatisticsService.getRevenueStatisticsByBatchIdAndRevenueKind(batchId, "收入");
        if (Objects.isNull(beanList) || beanList.size() == 0) {
            pieChart.setTitle("收入(暂无该批次收入统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(总收入),饼图数据(列,行)
        pieChart.setTitle("收入");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> totalProjectIncome = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();
        // 遍历项目统计记录(一条记录为一行)
        for (RevenueStatisticsBean bean : beanList) {
            PieChartDataRows pieChartDataRows = new PieChartDataRows();
            // 设置种类
            String kind = bean.getKind();
            pieChartDataRows.setOptions(kind);
            // 设置数目
            BigDecimal decimal = bean.getAmount();
            pieChartDataRows.setNumber(decimal.doubleValue());
            // 添加行(收入不等于0,才添加到饼图中)
            if (decimal.doubleValue() != 0) {
                rows.add(pieChartDataRows);
            }
            // 累加到总收入
            bigDecimal = BigDecimalUtils.add(decimal, bigDecimal);
        }
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        String s = bigDecimal.toString();
        totalProjectIncome.add(s);
        lists.add(totalProjectIncome);
        pieChart.setLists(lists);
        return pieChart;
    }


    /***
     * @Description: 获取收支比数据
     * 判断该批次营业分析记录是否存在
     * 判断该批次项目统计记录是否存在
     * 获取批次总收入,总支出
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-02 11:18:28
     */
    public PieChart getRevenueRatioData(String batch) {
        // 创建PieChart对象
        PieChart pieChart = new PieChart();
        // 判断该批次营业分析记录是否存在
        Integer batchId = existBatchByBatch(batch);
        if (Objects.isNull(batchId)) {
            pieChart.setTitle("收支比(暂无该批次营业分析记录)");
            return pieChart;
        }
        // 判断该批次收支统计记录是否存在
        List<RevenueStatisticsBean> beanList = revenueStatisticsService.getRevenueStatisticsByBatchIdAndRevenueKind(batchId, "收入");
        List<RevenueStatisticsBean> beanList2 = revenueStatisticsService.getRevenueStatisticsByBatchIdAndRevenueKind(batchId, "支出");
        // 既无收入记录也无支出记录
        if ((Objects.isNull(beanList) || beanList.size() == 0) && (Objects.isNull(beanList2) || beanList2.size() == 0)) {
            pieChart.setTitle("项目利润比(暂无该批次项目统计记录)");
            return pieChart;
        }
        // 创建标题,是否为空数据,详细记录(总收入),饼图数据(列,行)
        pieChart.setTitle("收支比");
        pieChart.setDataEmpty(false);
        List<List<String>> lists = new ArrayList<>();
        List<String> projectProfitRatio = new ArrayList<>();
        BigDecimal bigDecimal = new BigDecimal(0);
        PieChartData chartData = new PieChartData();
        List<String> columns = new ArrayList<>();
        columns.add("options");
        columns.add("number");
        chartData.setColumns(columns);
        List<PieChartDataRows> rows = new ArrayList<>();

        // 设置总收入
        PieChartDataRows row = new PieChartDataRows();
        BigDecimal decimal = revenueStatisticsService.getTotalAmountByRevenueStatisList(beanList); // 项目总收入
        row.setNumber(decimal.doubleValue());
        row.setOptions("总收入");
        rows.add(row);
        // 获取总支出
        PieChartDataRows row2 = new PieChartDataRows();
        BigDecimal decimal2 = revenueStatisticsService.getTotalAmountByRevenueStatisList(beanList2);
        ; // 项目总支出
        if(Objects.isNull(decimal2))
            decimal2 = new BigDecimal(0);
        row2.setNumber(decimal2.doubleValue());
        row2.setOptions("总支出");
        rows.add(row2);
        // 设置行
        chartData.setRows(rows);
        pieChart.setChartData(chartData);
        // 设置详细记录
        BigDecimal bigDecimal1 = decimal.subtract(decimal2); // 总利润
        String s = bigDecimal1.toString();
        projectProfitRatio.add(s);
        lists.add(projectProfitRatio);
        pieChart.setLists(lists);
        return pieChart;
    }


    /***
     * @Description: 获取员工利润排行数据
     * 通过批次获取批次id
     * 通过批次id从员工统计表中获取记录集合
     * 创建员工利润数据对象
     * 遍历该集合,填充对象并返回
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-06-03 10:20:00
     */
    public VChart getStaffData(String batch) {
        // 创建vchart对象
        VChart vChart = new VChart();

        if (Objects.isNull(batch)) {
            vChart.setTitle("员工利润排行(批次为空)");
            vChart.setDataEmpty(true);
            return null;
        }
        // 通过批次获取批次id
        Integer id = getIdByBatch(batch);
        if (Objects.isNull(id)) {
            vChart.setTitle("员工利润排行(本批次营业分析记录为空!)");
            vChart.setDataEmpty(true);
            return null;
        }
        // 通过批次id从员工统计表中获取记录集合
        List<EmployeeStatisticsBean> list = employeeStatisticsService.getEmployeeStatisticsByBatchId(id);
        if (Objects.isNull(list) || list.size() == 0) {
            vChart.setTitle("员工利润排行(本批次员工统计记录为空!)");
            vChart.setDataEmpty(true);
            return null;
        }
        // 设置标题和不为空
        vChart.setTitle("员工利润排行");
        vChart.setDataEmpty(false);
        // 声明行,填充列
        VChartData chartData = new VChartData();
        List<ChartDataRows> rows = new ArrayList<>();
        List<String> columns = new ArrayList<>();
        columns.add("name");
        columns.add("profit");
        columns.add("income");
        columns.add("expenditure");
        chartData.setColumns(columns);
        // 遍历集合,填充行
        for (EmployeeStatisticsBean bean : list) {
            String name = bean.getName();
            BigDecimal profit = bean.getProfit();
            BigDecimal income = bean.getIncome();
            BigDecimal expenditure = bean.getExpenditure();
            StaffProfitChartDataRows row = new StaffProfitChartDataRows();
            row.setName(name);
            row.setProfit(profit.doubleValue());
            row.setIncome(income.doubleValue());
            row.setExpenditure(expenditure.doubleValue());
            rows.add(row);
        }
        chartData.setRows(rows);
        vChart.setChartData(chartData);
        return vChart;
    }
}
